package com.ideaaedi.plugin.fastcoding.action;

import com.ideaaedi.commonds.bash.BashUtil;
import com.ideaaedi.commonds.exception.ExceptionUtil;
import com.ideaaedi.plugin.fastcoding.util.NotifyUtil;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * 杀进程
 *
 * @author JustryDeng
 * @since 2021/12/18 9:10:51
 */
public class KillProcessAction extends AnAction {
    
    public static final String ACTION_NAME = "ProcessKiller";
    
    @Override
    public void actionPerformed(AnActionEvent event) {
        Project project = event.getData(PlatformDataKeys.PROJECT);
        if (project == null) {
            return;
        }
        String port = Messages.showInputDialog(project, "Please input port", ACTION_NAME, Messages.getInformationIcon(),
                null, new InputValidator() {
            @Override
            public boolean checkInput(String inputText) {
                return NumberUtils.isDigits(inputText);
            }
        
            @Override
            public boolean canClose(String inputText) {
                return true;
            }
        });
        if (StringUtils.isNotBlank(port)) {
            try {
                final String response = BashUtil.killProcessByPorts(port);
                Messages.showInfoMessage(project, response, ACTION_NAME);
                // Messages.showDialog(project, response, ACTION_NAME, new String[]{CommonBundle.getOkButtonText()}, 0, Messages.getInformationIcon());
            } catch (Exception e) {
                NotifyUtil.notify(project, ACTION_NAME, ExceptionUtil.getStackTraceMessage(e), NotificationType.ERROR);
            }
        }
    }
}
